<template>

	<view class="before-order" @click="hidenMask">
		<!-- 加载动画 -->
		<loading ref="loading" :custom="false" :shadeClick="true" :type="1" :shadeShow="false">
		</loading>

		<view class="before-order-search-top">
			<view class="before-order-search-top-left">
				<image src="../../static/search.png" mode=""></image>
				<input type="text" v-model="search" confirm-type="search" @confirm="getSearch" placeholder="请输入搜索内容" />

				<button type="button" class="before-order-search-top-left-btn" @click="getSearch">搜索</button>

			</view>
			<view class="before-order-search-top-scan" @click="toOrder">
				新建
			</view>
		</view>

		<!-- 订单信息 -->
		<view class="before-order-content">
			<view class="before-order-content-item" v-for="(item,index) in list" :key='index'
				@click.stop="showChoice(index)">

				<view class="before-order-content-item-list" :class="'test'+index">
					<view>
						<text class="before-order-content-item-list-title">订单编号</text>
						<text class="before-order-content-item-list-text">{{item.batchCode}}</text>
					</view>
					<view>
						<text class="before-order-content-item-list-title">样本编号</text>
						<text class="before-order-content-item-list-text">{{item.sampleCodes}}</text>
					</view>
					<view>
						<text class="before-order-content-item-list-title">受检人</text>
						<text class="before-order-content-item-list-text">{{item.createdByName}}</text>
					</view>
					<view>
						<text class="before-order-content-item-list-title">检测项目</text>
						<text class="before-order-content-item-list-text">{{item.productNames}}</text>
					</view>
					<view>
						<text class="before-order-content-item-list-title">创建时间</text>
						<text class="before-order-content-item-list-text">{{item.createdTime}}</text>
					</view>
					<view>
						<text class="before-order-content-item-list-title">送检医院</text>
						<text class="before-order-content-item-list-text">{{item.hospitals}}</text>
					</view>
				</view>
				<view class="before-order-main-list-content-choice" v-if="item.selected" @click.stop="cancel(index)"
					:style="{height:`${eleHeight}px`}">
					<view class="before-order-main-list-content-choice-update" @click.stop="toOrder1(item)"
						style="color: rgba(255, 255, 255, 0.9)">
						修改
					</view>
					<view class="before-order-main-list-content-choice-delete" @click.stop="deleteItem(item)"
						style="color:  rgba(255, 255, 255, 0.9)">
						删除
					</view>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	import baseUrl from '../../utils/config.js'
	import loading from '../../components/xuan-loading/xuan-loading.vue'
	export default {
		components: {
			loading
		},

		data() {
			return {
				search: '',
				eleHeight: 0, // 遮罩高
				list: [], //暂存样本列表
				to: 0, //防止进入页面点击新建

			};
		},

		onLoad() {
			// this.getData()
		},

		onShow() {
			console.log(111)
			this.getData()
		},

		methods: {
			close: function() {
				this.$refs.loading.close();
			},
			open: function() {
				this.$refs.loading.open();
			},

			// 获取数据
			getData() {
				this.open();
				let url = "batch/batch-search"
				uni.request({
					url: baseUrl + url,
					data: {
						userId: uni.getStorageSync('userId'),
						selectCode: this.search,
						batchStatus:"未提交"
					},
					//formdate格式
					header: {
						"Content-Type": "application/json",
						'token': uni.getStorageSync('token')
					},
					method: "POST",
					success: (res) => {
						// console.log('订单list', res)
						this.close();
						if (res.data.status == 200) {
							this.list = res.data.data.map((item) => {
								item.selected = false
								return item
							})
						} else {
							let msg = res.data.msg
							uni.showToast({
								title: msg,
								duration: 2000,
								icon: 'none'
							});
						}
					},
					fail: (res) => {
						this.close();
						uni.showToast({
							title: '连接错误，请检查网络设置',
							duration: 2000,
							icon: 'none'
						});
					},
				})
			},

			// 点击任意地方关闭遮罩
			hidenMask() {
				this.list = this.list.map((el) => {
					el.selected = false;
					return el;
				})
			},

			// 点击弹出遮罩
			showChoice(index) {
				const query = uni.createSelectorQuery().in(this);

				query.select('.test' + index).boundingClientRect(data => {
					this.eleHeight = data.height
				}).exec();

				// 多选复选框改变事件
				for (let i = 0; i < this.list.length; i++) {
					if (i == index) {
						this.list[i].selected = true;
					} else {
						this.list[i].selected = false;
					}
				}

			},

			// 新建
			toOrder() {
				
					this.open();
					let url = "batch/create-batch"
					uni.request({
						url: baseUrl + url,
						data: {
							userinfo: uni.getStorageSync("userInfo")
						},
						//formdate格式
						header: {
							"Content-Type": "application/json",
							'token': uni.getStorageSync('token')
						},
						method: "POST",
						success: (res) => {
							console.log('新增订单', res)
							this.close();
							if (res.data.status == 200) {
								let data = JSON.stringify(res.data.data)
								uni.navigateTo({
									url: `../order/order?$data=${encodeURIComponent(data)}`
								})
							} else {
								let msg = res.data.msg
								uni.showToast({
									title: msg,
									duration: 2000,
									icon: 'none'
								});
							}
						},
						fail: (res) => {
							this.close();
							uni.showToast({
								title: '连接错误，请检查网络设置',
								duration: 2000,
								icon: 'none'
							});
						},
					})
				
			},

			// 修改
			toOrder1(item) {
				console.log('item',item)
				let data = JSON.stringify(item)
				uni.navigateTo({
					url: `../order/order?$data=${encodeURIComponent(data)}`
				})
			},

			// 删除
			deleteItem(item) {

				this.open();
				let url = "batch/batch-delete"
				uni.request({
					url: baseUrl + url,
					data: {
						id: item.id,
					},
					//formdate格式
					header: {
						"Content-Type": "application/json",
						'token': uni.getStorageSync('token')
					},
					method: "POST",
					success: (res) => {
						this.close();
						if (res.data.status == 200) {
							let msg = res.data.msg
							this.getData()
							uni.showToast({
								title: msg,
								duration: 2000,
								icon: 'none'
							});
						} else {
							let msg = res.data.msg
							uni.showToast({
								title: msg,
								duration: 2000,
								icon: 'none'
							});
						}
					},
					fail: (res) => {
						this.close();
						uni.showToast({
							title: '连接错误，请检查网络设置',
							duration: 2000,
							icon: 'none'
						});
					},
				})

			},

			// 关闭遮罩
			cancel(index) {
				this.list[index].selected = false;
			},

			//搜索功能
			getSearch() {
				this.getData()
			}
		},


	}
</script>

<style lang="scss">
	.before-order {
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		font-size: 32rpx;
		background-color: #f0f0f0;

		&-search-top {
			display: flex;
			align-items: center;
			margin: 20rpx;
			height: 60rpx;

			&-left {
				flex: 1;
				background-color: #fff;
				display: flex;
				align-items: center;
				height: 100%;
				border-radius: 30rpx;

				image {
					width: 40rpx;
					height: 40rpx;
					padding: 0 20rpx;
				}

				input {
					flex: 1;
				}

				&-btn {
					width: 150rpx;
					color: #689ae3;
					font-size: 32rpx;
					height: 100%;
					display: flex;
					align-items: center;
					justify-content: center;
					margin: 0;
					outline: none;
					background-color: #fff;
					border-radius: 30rpx;
				}
			}

			&-scan {
				margin: 0 20rpx;
				color: #689ae3;
			}

			button::after {
				border: none;
			}
		}

		&-main {
			height: calc(100vh - 100rpx);
			background-color: #fff;
			margin: 0 20rpx;
			border-radius: 15rpx;

			&-datepicker {
				display: flex;



				padding: 0 40rpx;
				align-items: center;
				background-color: #fff;
				height: 80rpx;
				line-height: 80rpx;


				.start_date {
					width: 270rpx;
					background-color: #f0f0f0;
					justify-content: center;

					display: flex;
					align-items: center;
					height: 60rpx;
					border-radius: 30rpx;

					.date {

						padding-left: 10rpx;
						flex: 1;

					}

					image {
						padding-left: 10rpx;
					}

				}

				&-choose {
					display: flex;
				}

				image {
					width: 36rpx;
					height: 36rpx;

					margin-right: 10rpx;

					transition-duration: 0.3s;
				}

				&-line {
					margin: 0 10rpx;
				}

				&-title {
					margin-right: 20rpx;
					color: rgba(35, 39, 55, 0.9);
				}
			}

			&-list {
				position: relative;

				image {
					width: 40rpx;
					height: 40rpx;
					padding-right: 20rpx;

				}

				&-content {
					margin: 20rpx;
					margin-left: 0;
					padding: 0 20rpx;


					&-title {
						height: 60rpx;
						display: flex;
						align-items: center;
					}

					&-info {
						height: 60rpx;
						display: flex;
						align-items: center;

						text {
							padding-right: 20rpx;
						}
					}

					&-choice {
						position: absolute;
						width: 100%;
						top: 0;
						left: 0;
						background-color: rgba(0, 0, 0, 0.4);
						height: 278rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						border-radius: 30rpx;

						&-update {
							width: 120rpx;
							height: 120rpx;
							background-color: orange;
							border-radius: 50%;
							display: flex;
							justify-content: center;
							align-items: center;
							margin-right: 50rpx;
						}

						&-delete {
							width: 120rpx;
							height: 120rpx;
							background-color: red;
							border-radius: 50%;
							justify-content: center;
							align-items: center;
							display: flex;
						}
					}
				}
			}
		}


		&-content {
			flex: 1;
			overflow: auto;

			&-item {
				display: flex;
				align-items: center;
				background-color: #fff;
				margin: 0 20rpx;
				margin-top: 20rpx;
				padding: 0 20rpx;
				font-size: 30rpx;
				border-radius: 30rpx;
				position: relative;

				&-dg {
					width: 80rpx;
					height: 50rpx;
					background-color: #0f90d1;
					border-top-right-radius: 23rpx 23rpx;
					border-bottom-left-radius: 30rpx 30rpx;
					top: 0;
					right: 0;
					position: absolute;
					color: #fff;
					font-size: 30rpx;
					text-align: center;

					.dg {
						width: 40rpx;
						height: 40rpx;
					}
				}


				&-checkbox {
					margin-right: 20rpx;
				}

				&-list {
					width: 100%;

					&-title {
						display: inline-block;
						width: 200rpx;
						color: rgba(35, 39, 55, .9);
					}

					&-text {
						color: rgba(35, 39, 55, .5);
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						display: inline-block;
						// width: 650px;
					}

					view {
						height: 60rpx;
						line-height: 60rpx;
						display: flex;
						justify-content: space-between;
					}
				}
			}


			.active {
				border: 6rpx solid #0f90d1;
			}
		}
	}
</style>
